package blogger;

import com.google.gdata.client.GoogleService;
import com.google.gdata.client.Query;
import com.google.gdata.data.DateTime;
import com.google.gdata.data.Entry;
import com.google.gdata.data.Feed;
import com.google.gdata.data.Person;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.TextContent;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

public class Blogger {

    private static final String METAFEED_URL = 
        "http://www.blogger.com/feeds/default/blogs";

    private static final String FEED_URI_BASE = "http://www.blogger.com/feeds";
    private static final String POSTS_FEED_URI_SUFFIX = "/posts/default";
    private static final String COMMENTS_FEED_URI_SUFFIX = "/comments/default";

    private static String feedUri;

    public Blogger() {
    }

    private static String getBlogId(GoogleService myService)
        throws ServiceException, IOException {
            // Get the metafeed
            final URL feedUrl = new URL(METAFEED_URL);
            Feed resultFeed = myService.getFeed(feedUrl, Feed.class);

            // If the user has a blog then return the id (which comes after 'blog-')
            if (resultFeed.getEntries().size() > 0) {
                Entry entry = resultFeed.getEntries().get(0);
                return entry.getId().split("blog-")[1];
            }
            throw new IOException("User has no blogs!");
    }

    public static ArrayList getBlogs(GoogleService myService)
        throws ServiceException, IOException {

        // Request the feed
        final URL feedUrl = new URL(METAFEED_URL);
        Feed resultFeed = myService.getFeed(feedUrl, Feed.class);
        ArrayList<String> blogs = new ArrayList();

        for (int i = 0; i < resultFeed.getEntries().size(); i++) {
            Entry entry = resultFeed.getEntries().get(i);
            blogs.add( entry.getTitle().getPlainText() );
        }

        return blogs;
    }

    public static ArrayList getPosts(GoogleService myService)
        throws ServiceException, IOException {
        // Request the feed
        URL feedUrl = new URL(feedUri + POSTS_FEED_URI_SUFFIX);
        Feed resultFeed = myService.getFeed(feedUrl, Feed.class);
        ArrayList<String> posts = new ArrayList();

        for (int i = 0; i < resultFeed.getEntries().size(); i++) {
            Entry entry = resultFeed.getEntries().get(i);
            posts.add( entry.getTitle().getPlainText() );
        }
        return posts;
    }

    public static Entry createPost(GoogleService myService, String title,
            String content, String authorName, String userName)
        throws ServiceException, IOException {
            // Create the entry to insert
            Entry myEntry = new Entry();
            myEntry.setTitle(new PlainTextConstruct(title));
            myEntry.setContent(new PlainTextConstruct(content));
            Person author = new Person(authorName, null, userName);
            myEntry.getAuthors().add(author);

            // Ask the service to insert the new entry
            URL postUrl = new URL(feedUri + POSTS_FEED_URI_SUFFIX);
            return myService.insert(postUrl, myEntry);
    }

    public static Entry updatePostTitle(GoogleService myService,
            Entry entryToUpdate, String newTitle) throws ServiceException,
           IOException {
               entryToUpdate.setTitle(new PlainTextConstruct(newTitle));
               URL editUrl = new URL(entryToUpdate.getEditLink().getHref());
               return myService.update(editUrl, entryToUpdate);
    }


    public static void deletePost(GoogleService myService, String editLinkHref)
        throws ServiceException, IOException {
        URL deleteUrl = new URL(editLinkHref);
        myService.delete(deleteUrl);
    }



    // This whole run method could just as easily become the backbone for our unit test
    // if we wanted to turn that in also
        
    public static void run(GoogleService myService, String userName,
            String userPassword) throws ServiceException, IOException {

        // Authenticate using ClientLogin
        myService.setUserCredentials(userName, userPassword);

        // Get the blog ID from the metatfeed.
        String blogId = getBlogId(myService);
        feedUri = FEED_URI_BASE + "/" + blogId;

        // Demonstrate retrieving a list of the user's blogs.
        getBlogs(myService);

        // Demonstrate how to publish a public post.
        //     Entry is GData datatype
        Entry publicPost = createPost(myService, "Hunting more trout",
                "<p>Them trout sure are stinky :(<p>",
                "Post author", userName);
        System.out.println("Successfully created public post: "
                + publicPost.getTitle().getPlainText());

        // Demonstrate various feed queries.
        getPosts(myService);
        publicPost = updatePostTitle(myService, publicPost, "The party's over");
        System.out.println("Post's new title is \""
                + publicPost.getTitle().getPlainText() + "\".\n");

        deletePost(myService, publicPost.getEditLink().getHref());
    }

    public static void main(String[] args) {

        String userName = "mrd.test.tube@gmail.com";
        String userPassword = "testing123";

        GoogleService myService = new GoogleService("blogger",
                "exampleCo-exampleApp-1");

        try {
            run(myService, userName, userPassword);
        } catch (ServiceException se) {
            se.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

}




